﻿using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.DataAccess.UserCode
{
    public partial class BsQuoteDataAccess
    {
        public static DataTable GetDataByID(string id)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    #region SQL Server
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    string strcmd = @"select * from V_Quote where QuoteID=@QuoteID";
                    dpc.AddWithValue("@QuoteID", id);

                    return DataAccessCommon.Select(strcmd, dpc).Tables[0];
                    #endregion
                }
                catch
                {
                    throw;
                }
            }
        }

        public static DataTable SelectList(string QuoteID, string ProductID, string ProductName, string From, string End, int pageIndex, int PageSize, out int m_count)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    #region SQL Server
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    string strcmd = @"select * from V_Quote o where 1=1";
                    if (!string.IsNullOrEmpty(QuoteID))
                    {
                        strcmd += " and o.QuoteID like @QuoteID";
                        dpc.AddWithValue("@QuoteID", "%" + QuoteID + "%");
                    }
                    if (!string.IsNullOrEmpty(ProductID)) 
                    {
                        strcmd += " and o.ProductID like @ProductID";
                        dpc.AddWithValue("@ProductID", "%" + ProductID + "%");
                    }
                    if (!string.IsNullOrEmpty(ProductName))
                    {
                        strcmd += " and o.ProductName like @ProductName";
                        dpc.AddWithValue("@ProductName", "%" + ProductName + "%");
                    }
                    if (!string.IsNullOrEmpty(From))
                    {
                        strcmd += " and o.BillDate >= @From";
                        dpc.AddWithValue("@From", From + " 00:00:00");
                    }
                    if (!string.IsNullOrEmpty(End))
                    {
                        strcmd += " and o.BillDate <= @End";
                        dpc.AddWithValue("@End", End + " 23:59:59");
                    }
                    strcmd += " order by o.BillDate desc";
                    return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, PageSize, dpc, out m_count);
                    #endregion
                }
                catch
                {
                    throw;
                }
            }
        }

        public static DataTable SelectDialogList(string strQuoteID, string strProductName, string OrderID, int pageIndex, int PageSize, out int m_count)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    #region SQL Server
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    string strcmd = @"select qu.*,c1.CustomerName,c2.CustomerName as SendCustomerName,c2.Addr as SendAddr,pa.ParameterName as CurrencyName
                                        from BS_Quote qu
                                        left join MD_Customer c1 on c1.CustomerID=qu.CustomerID
                                        left join MD_Customer c2 on c2.CustomerID=qu.SendCustomerID
                                        left join SYS_Parameter pa on pa.ParameterCode=qu.Currency and pa.ParameterTypeID='Currency'
                                        where qu.[Status]='1' and qu.QuoteID not in (select QuoteID from BS_Order where OrderID<>@OrderID and (QuoteID<>'' or QuoteID is not null))";
                    dpc.AddWithValue("@OrderID", OrderID);
                    if (!string.IsNullOrEmpty(strQuoteID))
                    {
                        strcmd += " and qu.QuoteID like @QuoteID";
                        dpc.AddWithValue("@QuoteID", "%" + strQuoteID + "%");
                    }
                    if (!string.IsNullOrEmpty(strProductName))
                    {
                        strcmd += " and qud.ProductName like @ProductName";
                        dpc.AddWithValue("@ProductName", "%" + strProductName + "%");
                    }
                    strcmd += " order by qu.BillDate desc";
                    return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, PageSize, dpc, out m_count);
                    #endregion
                }
                catch
                {
                    throw;
                }
            }
        }
    }
}
